package com.yunhuajinfu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.yunhuajinfu.R;
import com.yunhuajinfu.base.BaseActivity;
import com.yunhuajinfu.dialog.ExitLoaginDialog;
import com.yunhuajinfu.entity.Huifu;
import com.yunhuajinfu.http.HttpCallBack;
import com.yunhuajinfu.http.HttpUtils;
import com.yunhuajinfu.mian.Config;
import com.yunhuajinfu.response.CitizenscertificationRes;
import com.yunhuajinfu.tool.AbActivityManager;
import com.yunhuajinfu.tool.StatusBarUtil;
import com.yunhuajinfu.util.HtmlFormUtil;
import com.yunhuajinfu.view.TitleBar;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * yihuangxing   账户设置
 */
public class SeetingsActivity extends BaseActivity {
    @Bind(R.id.titlebar)
    TitleBar titlebar;
    @Bind(R.id.tv_exit_login)
    TextView tvExitLogin;
    @Bind(R.id.mViewNeedOffset)
    LinearLayout mViewNeedOffset;


    private int type = 0;    //0--表示真实姓名认证   1--表示交易密码认证
    private  ExitLoaginDialog exitLoaginDialog=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_seetings);
        StatusBarUtil.setTranslucentForImageView(this,0,mViewNeedOffset);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        titlebar.setTitleBar(R.drawable.back_selector, "账户设置", 0);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void setData() {

    }


    @OnClick({R.id.titlebar_iv_back, R.id.lly_zhenshi_name,
            R.id.lly_phone_renzheng, R.id.lly_emils_renzheng,
            R.id.lly_long_or_reger, R.id.lly_jiaoyi_pasd, R.id.tv_exit_login})
    public void widgetClick(View view) {
        switch (view.getId()) {

            case R.id.titlebar_iv_back:

                finish();

                break;
            case R.id.lly_zhenshi_name:     //真实姓名  实名认证
                type = 0;
                getAccountConfig(0);
                break;

            case R.id.lly_phone_renzheng:

                startActivity(new Intent(SeetingsActivity.this, PhoneRenZhengActivity.class));
                break;
            case R.id.lly_emils_renzheng:  //邮箱认证

//                startActivity(new Intent(SeetingsActivity.this, EmailRenZhengActivity.class));
                certificationEmils();


                break;
            case R.id.lly_long_or_reger:   // 修改登录密码
                startActivity(new Intent(SeetingsActivity.this, LoginPasswordActivity.class));
                break;
            case R.id.lly_jiaoyi_pasd:   //修改交易密码

                startActivity(new Intent(SeetingsActivity.this, JiaoYiPasswordActivity.class));
                //先判断是否实名认证过
//                type = 1;
//                getAccountConfig(1);
//
                break;


            case R.id.tv_exit_login:    //退出登录

                  exitLoaginDialog = new ExitLoaginDialog(SeetingsActivity.this);


                exitLoaginDialog.setContent("您确定要退出登录吗 ?", "退出将清空用户登录信息", "确定", "取消");

                exitLoaginDialog.setCancleListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        exitLoaginDialog.dismiss();
                    }
                });


                exitLoaginDialog.setOKListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        loginOut(exitLoaginDialog);


                    }
                });

                exitLoaginDialog.show();


                break;

            default:
        }
    }

    /**
     * 退出登录
     */

    private void loginOut(final ExitLoaginDialog exitLoaginDialog) {
        HttpParams params = new HttpParams();
        HttpUtils.post(Config.LOGINOUT, params, new HttpCallBack(this, "正在退出...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
                exitLoaginDialog.dismiss();
                //空用户登录信息
                mpreferencesUtils.remove(SeetingsActivity.this, "userAccount");
                mpreferencesUtils.remove(SeetingsActivity.this,"token");
                mpreferencesUtils.remove(SeetingsActivity.this,"jsessionid");
                //回到登录页面
                Intent intent = new Intent(SeetingsActivity.this, LoginActivity.class);
                intent.putExtra("flag", 110);
                startActivity(intent);
                //清空所有activity   放这里解决退出白屏效果
                AbActivityManager.getInstance().clearAllActivity();
                finish();

            }

            @Override
            public void onError(String code, String message) {
                Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();

            }
        });


    }

    /**
     * 邮箱认证
     */
    private void certificationEmils() {
        HttpUtils.get(Config.TOMAIL, new HttpCallBack(this, "验证中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

//                if (code.equals("-1")) {
//                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
//
//                } else if (code.equals("-2")) {
//                    startActivity(new Intent(SeetingsActivity.this, EmailRenZhengActivity.class));
//                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
//                } else if (code.equals("1")) {
//                    startActivity(new Intent(SeetingsActivity.this, EmailRenZhengActivity.class));
//                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
//
//                } else if (code.equals("2")) {
//
//                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
//                    Intent intent=new Intent(SeetingsActivity.this, EmailRenZhengActivity.class);
//
//                    startActivity(intent);
//                }
            }

            @Override
            public void onError(String code, String message) {

                //-2未申请  -1申请中  1未通过   2通过

                //未通过,和未申请进行下一步

                if (code.equals("-1")) {
                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();

                } else if (code.equals("-2")) {
                    startActivity(new Intent(SeetingsActivity.this, EmailRenZhengActivity.class));
                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
                } else if (code.equals("1")) {
                    startActivity(new Intent(SeetingsActivity.this, EmailRenZhengActivity.class));
                    Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();

                } else if (code.equals("2")) {
                    Intent intent=new Intent(SeetingsActivity.this, EmailRenZhengActivity.class);
                    intent.putExtra("email",100);
                    intent.putExtra("message",message);
                    startActivity(intent);
                    Toast.makeText(SeetingsActivity.this, "邮箱认证已通过", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }


    /**
     * 实名认证
     */
    private void getAccountConfig(final int type) {

        HttpParams params = new HttpParams();

        HttpUtils.post(Config.ACCOUNTCONFIG, params, new HttpCallBack(this, "认证中...") {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {

                if (null != result) {
                    try {
                        CitizenscertificationRes res = new CitizenscertificationRes(new JSONObject(result.toString()));
                        Huifu info = null;
                        if (null != res.getHuifu()) {
                            info = res.getHuifu();
                            Map<String, String> params = new HashMap<String, String>();
                            params.put("capAcntNo", info.getCapAcntNo());
                            params.put("user_id_from", info.getUserIdFrom());
                            params.put("back_notify_url", info.getBackNotifyUrl());
                            params.put("city_id", info.getCityId());
                            params.put("email", info.getEmail());
                            params.put("cust_nm", info.getCustNm());
                            params.put("mchnt_cd", info.getMchntCd());
                            params.put("certif_id", info.getCertifId());
                            params.put("parent_bank_id", info.getParentBankId());
                            params.put("bank_nm", info.getBankNm());
                            params.put("mchnt_txn_ssn", info.getMchntTxnSsn());
                            params.put("mobile_no", info.getMobileNo());
                            //增加两个字段
                            params.put("ver", info.getVer());
                            params.put("page_notify_url", info.getPageNotifyUrl());
                            //
                            params.put("signature", URLEncoder.encode(info.getSignature(), "UTF-8"));

                            //去web加载页面
                            Intent intent = new Intent(SeetingsActivity.this, WebActivity.class);
                            intent.putExtra("url", info.getGateWayUrl());
                            intent.putExtra("postData", HtmlFormUtil.makePostHTML(info.getGateWayUrl(), params));
                            intent.putExtra("titile_text", "实名认证");
                            try {
                                intent.putExtra("params", HtmlFormUtil.paramMap2bytes(params));
                                startActivity(intent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            //
                            switch (type) {

                                case 0:
                                    Intent intent = new Intent(SeetingsActivity.this, ZhenShiNameActivity.class);
                                    intent.putExtra("realName", res.getRealName());
                                    intent.putExtra("cardNum", res.getCardNum());
                                    startActivity(intent);
                                    break;

                                case 1:

//                                    startActivity(new Intent(SeetingsActivity.this, JiaoYiPasswordActivity.class));
                                    break;

                                default:
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(String code, String message) {

                Toast.makeText(SeetingsActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       this.exitLoaginDialog=null;
        this.activity=null;

    }
}
